package com.smart.campus.school.wx.cp.api;

import com.alibaba.fastjson.JSON;
import com.smart.campus.school.domain.Student;
import com.smart.campus.school.wx.cp.Common;
import com.smart.campus.school.wx.cp.config.WxCpTpConfiguration;
import com.smart.campus.school.wx.cp.model.WXStudent;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpTpService;

import java.util.ArrayList;
import java.util.List;


/**
 * 微信学生处理接口
 */
@Slf4j
public class StudentApi {

    /**
     * 创建学生
     * @param student
     * @return
     */
    public static String createStudent(Student student){

        try {
            WxCpTpService wxCpTpService = WxCpTpConfiguration.getCpTpService(Common.Constant.AGENTID);
            List<Long> deptIdList = new ArrayList<>();
            deptIdList.add(student.getClassId());
            WXStudent wxStudent = new WXStudent(student.getId().toString(),student.getStudentName(),deptIdList);

            log.info("开始微信添加学生:{}",wxStudent);
            String result = wxCpTpService.post(Common.Constant.CREATE_STUDENT_URL_PREFIX+"access_token"+Common.Constant.TOKEN, JSON.toJSONString(wxStudent));
            log.info("添加学生，微信返回结果:{}",result);
            return result;
        } catch (Exception e) {
            log.info("微信添加学生异常",e);
        }
        return null;
    }

    /**
     * 更新学生
     * @param student
     * @return
     */
    public static String updateStudent(Student student){

        try {
            WxCpTpService wxCpTpService = WxCpTpConfiguration.getCpTpService(Common.Constant.AGENTID);
            List<Long> deptIdList = new ArrayList<>();
            deptIdList.add(student.getClassId());
            WXStudent wxStudent = new WXStudent(student.getId().toString(),student.getStudentName(),deptIdList);

            log.info("开始微信更新学生:{}",wxStudent);
            String result = wxCpTpService.post(Common.Constant.UPDATE_STUDENT_PREFIX+"access_token"+Common.Constant.TOKEN, JSON.toJSONString(wxStudent));
            log.info("更新学生，微信返回结果:{}",result);
            return result;
        } catch (Exception e) {
            log.info("微信更新学生异常",e);
        }
        return null;
    }

    /**
     * 删除学生
     * @param student
     * @return
     */
    public static String deleteStudent(Student student){

        try {
            WxCpTpService wxCpTpService = WxCpTpConfiguration.getCpTpService(Common.Constant.AGENTID);
            List<Long> deptIdList = new ArrayList<>();
            deptIdList.add(student.getClassId());
            WXStudent wxStudent = new WXStudent();
            wxStudent.setStudentUserId(student.getId().toString());
            log.info("微信删除学生:{}",wxStudent);
            String result = wxCpTpService.post(Common.Constant.DELETE_STUDENT_PREFIX+"access_token"+Common.Constant.TOKEN, JSON.toJSONString(wxStudent));
            log.info("删除学生，微信返回结果:{}",result);
            return result;
        } catch (Exception e) {
            log.info("微信删除学生异常",e);
        }
        return null;
    }
}
